﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Com.Crc.ResManage.BLL.EquipManage;
using Com.Crc.PoJo.Model.Resource;
using Com.App_Code;
using System.Data;
using System.Collections;
using Infragistics.WebUI.UltraWebGrid;

namespace Com.Crc.ResManage.Web.EquipManage.Exchange
{
    public partial class AddExchange : BasePage
    {
        BroadBandManageExchange manageExchange = new BroadBandManageExchange();

        BroadBandManageExchangeUserBorad broadBandManageExchangeUserBorad = new BroadBandManageExchangeUserBorad();
        public string EquipID
        {
            get
            {
                if (ViewState["EquipID"] != null)
                {
                    return ViewState["EquipID"].ToString();
                }

                return "";
            }
            set
            {
                ViewState["EquipID"] = value;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                manageExchange = new BroadBandManageExchange();
                InitControl();
            }
        }
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            manageExchange = new BroadBandManageExchange();
            string strBranchOfficeID = "";
            string strOfficeID = "";
            if (this.ddlBranchOffice.SelectedIndex != -1)
            { 
                strBranchOfficeID = this.ddlBranchOffice.Items[this.ddlBranchOffice.SelectedIndex].Value;
            }
            if (this.ddlOffice.SelectedIndex != -1)
            {
                strOfficeID = this.ddlOffice.Items[this.ddlOffice.SelectedIndex].Value;
            }
            DataTable dt = manageExchange.QueryExchangeData(strBranchOfficeID, strOfficeID, "");
            BindData(dt);

        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            ArrayList arrEquipID = new ArrayList();
            foreach (UltraGridRow gvRow in this.grd.DisplayLayout.Rows)
            {
                if (gvRow.Cells.FromKey("CHECK").ToString().ToUpper() == "TRUE")
                {
                    int ID = int.Parse(gvRow.Cells.FromKey("n_id").ToString());
                    arrEquipID.Add(ID.ToString());
                }
            }

            if (arrEquipID.Count == 0)
            {
                ShowAlertForUpdatePanel("请选择设备!");
                return;
            }

            manageExchange = new BroadBandManageExchange();
            manageExchange.DeleteExchangeData(arrEquipID);
            btnQuery_Click(null, null);
            ShowAlertForUpdatePanel("设备删除成功!");
        }
        private void InitControl()
        {
            this.btnAdd.Attributes.Add("onclick", "return OpenAddWindow();");
            this.btnDelete.Attributes.Add("onclick", "return window.confirm('确认删除所选用户板信息?')");
            if (Request.QueryString["equipid"] != null)
            {
                EquipID = Request.QueryString["equipid"].ToString();
            }
            //初始化各个下拉框
            InitControlHelp.LoadBranchInfo(this.ddlOffice);
            InitControlHelp.LoadCommonDicInfo(this.ddlBranch, Dictionary.COMMON_DIC_TYPE.RESOURCE_EXCHANGE_BRANCHOFFICE); 
            InitControlHelp.LoadCommonDicInfo(this.upexchange, Dictionary.COMMON_DIC_TYPE.RESOURCE_EXCHANGE_EQUITMENT_TYPE);
            InitControlHelp.LoadCommonDicInfo(this.unittypeid, Dictionary.COMMON_DIC_TYPE.RESOURCE_EXCHANGE_EQUITMENT_TYPE);
            if (Request.QueryString["optype"] != null && Request.QueryString["optype"].ToString() == "0")
            {
                LoadEquipData();
                LoadUserBoardData();
            }
        }
        /// <summary>
        /// 加载设备下用户板信息
        /// </summary>
        private void LoadUserBoardData()
        {
            broadBandManageExchangeUserBorad = new BroadBandManageExchangeUserBorad();
            DataTable dt = broadBandManageExchangeUserBorad.QueryExchangeUserBoradData("", "", this.EquipID);
            BindData(dt);
        }
        private void BindData(DataTable dt)
        {
            grd.DataSource = dt;
            grd.DataBind();
        }
        /// <summary>
        /// 修改设备信息时初始化控件数据
        /// </summary>
        private void LoadEquipData()
        {
            DataTable dt = manageExchange.QueryExchangeData("", "", Request.QueryString["equipid"].ToString());
            InitControlHelp.SetSelectedValue(this.ddlBranch, dt.Rows[0]["N_DEP_BRANCH_ID"].ToString());
            InitControlHelp.SetSelectedValue(this.ddlOffice, dt.Rows[0]["N_SUBSTATION_ID"].ToString());
            InitControlHelp.SetSelectedValue(this.upexchange, dt.Rows[0]["N_M_UP_EXCHANGE_ID"].ToString());
            InitControlHelp.SetSelectedValue(this.unittypeid, dt.Rows[0]["N_UNITTYPE_ID"].ToString());

            this.textname.Text = dt.Rows[0]["S_NAME"].ToString();
            this.text2m.Text = dt.Rows[0]["N_2M"].ToString();
            this.textexchangeport.Text = dt.Rows[0]["N_M_UP_EXCHANGE_PORT"].ToString();
            this.textes.Text = dt.Rows[0]["N_M_START_NO"].ToString();
            this.textee.Text = dt.Rows[0]["N_M_END_NO"].ToString();
            this.textbak.Text = dt.Rows[0]["S_BAK"].ToString();
        }
        protected void btn_Search_Click(object sender, EventArgs e)
        {
            LoadUserBoardData();
        }
        /// <summary>
        /// 新增或修改保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            manageExchange = new BroadBandManageExchange();
            Com.Crc.PoJo.Model.Resource.Exchange exchange = new Com.Crc.PoJo.Model.Resource.Exchange();
            exchange.N_UNITTYPE_ID = long.Parse(unittypeid.SelectedValue);
            exchange.S_NAME = textname.Text;
            exchange.N_2M = long.Parse(text2m.Text);
            exchange.N_M_UP_EXCHANGE_ID = long.Parse(upexchange.SelectedValue);
            exchange.N_M_UP_EXCHANGE_PORT = long.Parse(textexchangeport.Text);
            exchange.N_M_START_NO = long.Parse(textes.Text);
            exchange.N_M_END_NO = long.Parse(textee.Text);
            exchange.N_M_END_NO = long.Parse(textbak.Text);

            if (Request.QueryString["optype"] != null && Request.QueryString["optype"].ToString() == "0")
            {
                //修改
                exchange.N_ID = long.Parse(this.EquipID);
                manageExchange.UpdateExchangeData(exchange);
                base.ShowAlertForUpdatePanel("设备修改成功！");
            }
            else
            {
                //新增
                manageExchange.AddExchangeData(exchange);
                base.ShowAlertForUpdatePanel("设备新增成功！");
                this.btnAdd.Enabled = false;
            }
        }

        protected void grd_InitializeLayout(object sender, Infragistics.WebUI.UltraWebGrid.LayoutEventArgs e)
        {
            grd.Bands[0].Columns.Clear();
            int i = 0;
            base.AddGridCheckColumn(this.grd, i++, grd.ClientID);
            base.AddGridColumn(this.grd, "N_ID", "N_ID", 80, i++, true);
            base.AddGridColumn(this.grd, "用户板数量", "N_CAP", 90, i++, false);
            base.AddGridColumn(this.grd, "用户板类型", "N_TYPE", 100, i++, false);
            base.AddGridColumn(this.grd, "起始端口号", "N_PORT_S", 80, i++, false);
            base.AddGridColumn(this.grd, "结束端口号", "N_PORT_E", 80, i++, false);
            base.AddGridColumn(this.grd, "起始槽位号", "N_TREN_S", 80, i++, false);
            base.AddGridColumn(this.grd, "结束槽位号", "N_TREN_E", 80, i++, false);
            base.AddGridColumn(this.grd, "起始设备号/V5接口号", "N_MACHINE_S", 80, i++, false);
            base.AddGridColumn(this.grd, "结束设备号/V5接口号", "N_MACHINE_E", 80, i++, false);

            base.AddGridImageColumn(grd, "编辑", "Edit", 40, i++, "~/img/icon_edit.gif");

            this.grd.Columns.FromKey("N_CAP").Width = Unit.Percentage(100);
        }

        protected void grd_PageIndexChanged(object sender, Infragistics.WebUI.UltraWebGrid.PageEventArgs e)
        {

        }
    }
}